package infb.servlet.movil;

import infb.dao.CitasInterfaz;

import infb.dao.MedicosInterfaz;
import infb.modelo.Citas;
import infb.modelo.Medicos;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.sql.Connection;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CitasServlet extends HttpServlet {


    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, NamingException {
        System.out.println("Has entrado al Servlet") ;
        DataOutputStream dataOut = new DataOutputStream(response.getOutputStream());
        DataInputStream dis = new DataInputStream(request.getInputStream());
        Context contexto = new InitialContext();
        CitasInterfaz citaInterfaz = (CitasInterfaz) contexto.lookup("CitasEJB");
        MedicosInterfaz medicoInterfaz = (MedicosInterfaz) contexto.lookup("MedicosEJB");
        String perfil=dis.readUTF();
        if(perfil.equals("Paciente"))
        {
        String paciente = dis.readUTF();
        List<Citas> listaCitas = null;
        listaCitas = citaInterfaz.citasMovil(paciente);
        EnviarCitas(listaCitas, dataOut);
        }
        else if (perfil.equals("Medico")){
        String dni=dis.readUTF();
        System.out.println("El dni recibido es "+dni);
        List<Citas> listaCitas = null;
        listaCitas = citaInterfaz.citasMovilMedico((String)dni);
        EnviarCitas(listaCitas, dataOut);
        }
        dataOut.flush();
        dataOut.close();

    }

    private void EnviarCitas(List<Citas> listaCita, DataOutputStream dataOut) throws IOException {

        int longitud = listaCita.size();
        String hora, fechaCad;
        Date fecha;
        Citas cita;
        dataOut.write(longitud);
        for (int i = 0; i < longitud; i++) {
            cita = listaCita.get(i);
            fecha = cita.getFechaFinalizacion();
            hora = cita.getHoraFinalizacion();

            SimpleDateFormat dateformat = new SimpleDateFormat("dd/MM/yyyy");
            fechaCad = new String(dateformat.format(fecha));
            System.out.println("Fecha    :" + fechaCad);
            System.out.println("Hora    :" + hora);
            dataOut.writeUTF(fechaCad);
            dataOut.writeUTF(hora);
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(MedicamentosServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (NamingException ex) {
            Logger.getLogger(MedicamentosServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
